package com.ambow.shop.action.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.shop.action.Action;
import com.ambow.shop.domain.Product;
import com.ambow.shop.domain.ProductForm;
import com.ambow.shop.service.ProductService;
import com.ambow.shop.service.impl.ProductServiceImpl;

/**
 * 编辑产品业务流程。
 * 
 * @author chunw
 *
 */
public class EditProductAction implements Action {
	private ProductService productService = new ProductServiceImpl();

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) {
		Integer productId = Integer.parseInt(request.getParameter("id"));
		Product product = productService.getProduct(productId);
		request.setAttribute("product", convert(product));
		return "../view/productEdit.jsp";
	}

	private ProductForm convert(Product product) {
		return new ProductForm(Integer.toString(product.getId()), product.getName(), product.getDescription(),
				Double.toString(product.getPrice()), product.getImage(),
				Integer.toString(product.getCategory().getId()));
	}

}
